/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.edu.unju.fi.reservahabitaciones.grupo14.struts.modelo.dao.impl;

import ar.edu.unju.fi.reservahabitaciones.grupo14.struts.modelo.dao.HuespedDao;
import ar.edu.unju.fi.reservahabitaciones.grupo14.struts.hibernate.HibernateUtil;
import java.util.List;
import ar.edu.unju.fi.reservahabitaciones.grupo14.struts.modelo.dominio.Huesped;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author mauriljb
 */
public class HuespedDaoImpl implements HuespedDao {
    @Override
    public List<Huesped> getAll() {
        Session session = ar.edu.unju.fi.reservahabitaciones.grupo14.struts.hibernate.HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Criteria criteria = session.createCriteria(Huesped.class);
            criteria.addOrder(Order.asc("huespedId"));
            List<Huesped> lista =  criteria.list();
            session.close();
            return lista;
    }
    
    @Override
    public void insert(Huesped huesped) {
          Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(huesped);
            session.getTransaction().commit();
            session.close();
    }
    
    @Override
     public void delete(Huesped huesped) {
          Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(huesped);
            session.getTransaction().commit();
            session.close();
    }
    @Override
      public void update(Huesped huesped) {
          Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(huesped);
            session.getTransaction().commit();
            session.close();
    }
    @Override
       public Huesped getHuesped(long id) {
       Session session = HibernateUtil.getSessionFactory().openSession();
       session.beginTransaction();
       Criteria criteria = session.createCriteria(Huesped.class);
       criteria.add(Restrictions.eq("id", id));
       Huesped huesped = (Huesped) criteria.list().get(0);
       session.close();
       return huesped;
       }
    
     @Override
    public Huesped gethuespedByDni(long dni){
      Session session = HibernateUtil.getSessionFactory().openSession();      
      Criteria criteria = session.createCriteria(Huesped.class);
      criteria.add(Restrictions.eq("dni", dni));
     
      return (Huesped) criteria.uniqueResult();
    }
    
}
